package com.justgame.project.generator.container;

import com.justgame.project.generator.enums.TemplateType;
import com.justgame.project.generator.schema.Entity;
import lombok.Builder;
import lombok.Getter;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Getter
@Builder
public class EntityContainer {
    /**
     * 生成代码执行后覆盖已存在的类型文件
     */
    private final List<TemplateType> coverExistTypes;
    private final List<Entity> entities;

    public Optional<Entity> findEntity(Predicate<? super Entity> filter){
       return entities.stream().filter(filter).findFirst();
    }

    public Optional<Entity> findEntityByTableName(String tableName){
        return entities.stream().filter(entity -> entity.getTableName().equals(tableName)).findFirst();
    }

    public boolean contains(String tableName){
        return entities.stream().anyMatch((entity)->entity.getTableName().equals(tableName));
    }

    public List<String> entityTableNames(){
        return entities.stream().map(Entity::getTableName).collect(Collectors.toList());
    }

    public boolean needCover(TemplateType templateType){
        return coverExistTypes.contains(templateType);
    }
}
